Imports System.Text

''' <summary>
''' Text helper class.
''' </summary>
''' <remarks></remarks>
Public NotInheritable Class StringHelper

  ''' <summary>
  ''' Fixes string for input into SQL.
  ''' </summary>
  ''' <param name="value">Text to fix.</param>
  ''' <returns>Fixed string.</returns>
  ''' <remarks>Replaces single quote with double quote.</remarks>
  Shared Function SqlEncode(ByVal value As String) As String

    If String.IsNullOrEmpty(value) Then
      Return String.Empty
    End If

    Return value.Replace("'", "''")

  End Function

  ''' <summary>
  ''' HTML encodes text.
  ''' </summary>
  ''' <param name="value">Text to html encode.</param>
  ''' <returns>HTML encoded text.</returns>
  ''' <remarks></remarks>
  Shared Function HtmlEncode(ByVal value As String) As String

    'Encode using method from Web class
    Return Web.WebHelper.HtmlEncode(value)

  End Function

  ''' <summary>
  ''' Url encodes text.
  ''' </summary>
  ''' <param name="value"><see cref="String">Text</see> to url encode.</param>
  ''' <returns></returns>
  ''' <remarks></remarks>
  Shared Function UrlEncode(ByVal value As String) As String

    Return Web.WebHelper.UrlEncode(value)

  End Function

  ''' <summary>
  ''' Decodes html to text.
  ''' </summary>
  ''' <param name="html">HTML to decode.</param>
  ''' <returns>Decoded HTML.</returns>
  ''' <remarks></remarks>
  Public Shared Function HtmlDecode(ByVal html As String) As String

    Return Web.WebHelper.HtmlDecode(html)

  End Function

  ''' <summary>
  ''' Correctly compares two strings.
  ''' </summary>
  ''' <param name="original">First string to compare.</param>
  ''' <param name="compareTo">Second string to compare.</param>
  ''' <returns>True if strings are the same.</returns>
  ''' <remarks></remarks>
  Public Shared Function Compare(ByVal original As String, ByVal compareTo As String) As Boolean

    If String.IsNullOrEmpty(original) Then
      Throw New ArgumentNullException("original")
    End If

    If String.IsNullOrEmpty(compareTo) Then
      Throw New ArgumentNullException("compareTo")
    End If

    If original.Length <> compareTo.Length Then
      Return False
    End If

    If String.CompareOrdinal(original, compareTo) >= 0 Then
      Return True
    Else
      Return False
    End If

  End Function

  ''' <summary>
  ''' Converts RTF to text.
  ''' </summary>
  ''' <param name="value">RTF to convert.</param>
  ''' <returns>Converted RTF as <seealso cref="String"/></returns>
  ''' <remarks></remarks>
  Public Shared Function ConvertRtfToText(ByVal value As String) As String
    Dim result As String = String.Empty

    Using converter As New System.Windows.Forms.RichTextBox()

      converter.Rtf = value
      result = converter.Text

    End Using

    Return result

  End Function

  ''' <summary>
  ''' Creates a Guid
  ''' </summary>
  ''' <returns>Guid as text</returns>
  ''' <remarks></remarks>
  Public Shared Function CreateGuid() As String

    Return Guid.NewGuid.ToString

  End Function

  Private Sub New()
  End Sub

End Class
